package work817;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentBiz {
  public static List<Student> list = new ArrayList<>();

  public static void exists(File file){
       if (file.exists()){
         System.out.println("文件已存在");
         show(file);
       }else {
         try {
           file.createNewFile();
           System.out.println("创建该文件成功");
           add(file);
         } catch (IOException e) {
           e.printStackTrace();
         }

       }
  }

  public static void show(File file){
    load(file);
    System.out.println("***学生基本信息***");
    for (Student student : list) {
      System.out.println("编号\t\t姓名\t\t身份证");
      System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getIdCard());
      System.out.println("***学生成绩***");
      System.out.println("科目\t\t成绩");
      System.out.println("Java成绩\t"+student.getJavaScore());
      System.out.println("Jsp成绩\t"+student.getJSPScore());
      System.out.println("Sql成绩\t"+student.getSQLScore());
      System.out.println("总成绩："+(student.getJavaScore()+student.getJSPScore()+student.getSQLScore()));
    }
  }


  public static void add(File file){
    Scanner sc = new Scanner(System.in);
    System.out.println("***输入学生信息***");
    System.out.println("学生编号：");
    int id = sc.nextInt();
    System.out.println("姓名：");
    String name = sc.next();
    System.out.println("身份证号：");
    String IdCard = sc.next();
    System.out.println("java成绩");
    int JavaScore = sc.nextInt();
    System.out.println("JspScore成绩:");
    int JspScore = sc.nextInt();
    System.out.println("Sql成绩：");
    int SqlScore = sc.nextInt();
    list.add(new Student(id,name,IdCard,JavaScore,JspScore,SqlScore));
    save(file);
  }

  /**
   * 序列化
   * @param file
   */
  private static void save(File file){
    try {
      ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
      oos.writeObject(list);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * 反序列化
   * @param file
   */
  private static void load(File file){
    try{
          ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
          list = (List<Student>) ois.readObject();
    }catch (Exception e){
       e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    File file = new File("E:\\bdqn\\javaDemo\\save.txt");
    exists(file);
  }
}
